package henry.lucky.alphay;

import henry.lucky.alphay.Domain.Prize;
import henry.lucky.alphay.Domain.PrizeCard;
import henry.lucky.alphay.db.DataBase;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class ResultLucky {

	private List<Prize> prizes;
	private List<PrizeCard> prizeCards;
	private DataBase dataBase=new DataBase();
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;
	private JTextField textField;
	private JTable table;
	private String[][] rowDatas;
	private DefaultTableModel model=null;
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void showFrame(){
		
		JFrame frame;
		frame=new JFrame("抽奖结果查询");
		frame.setSize(800, 600);
		frame.setVisible(true);
		//框架布局
		Container container=frame.getContentPane();
		JPanel panelTop=new JPanel();	
		JPanel panelCenter=new JPanel();
		panelCenter.setLayout(new BorderLayout());
		JPanel panelDown=new JPanel();
		container.add("Center",panelCenter);
		container.add("North", panelTop);
		container.add("South",panelDown);
		//设置标题
		JLabel labelTop=new JLabel("抽奖结果查询");
		panelTop.add(labelTop);
		//设置显示区域		
		prizeCards= dataBase.findPrizeCards();
		final Object[] colNames={"奖券编号","奖项"};		
		rowDatas = new String[prizeCards.size()][2];
		int rows=0;
		for(PrizeCard prizeCard:prizeCards){
			
			rowDatas[rows][0]=prizeCard.getCardName();
			rowDatas[rows][1]=prizeCard.getPrizeName();
			rows++;

		}
		model=new DefaultTableModel(rowDatas, colNames);
		table=new JTable(model);	
		panelCenter.add(new JScrollPane(table),BorderLayout.CENTER);
		//显示查询区域
		
		JLabel labelPrize=new JLabel("奖项");
		comboBox=new JComboBox(new String[]{""});
		JLabel label=new JLabel("奖券编号:");
		textField=new JTextField("",10);
		JButton button=new JButton("查询");
		prizes=dataBase.findCurrentLuckyPrize();
		for(Prize prize:prizes){
			comboBox.addItem(prize.getPrizeName());
		}
		panelDown.add(labelPrize);
		panelDown.add(comboBox);
		panelDown.add(label);
		panelDown.add(textField);
		panelDown.add(button);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String CardName=textField.getText();
				String PrizeName=comboBox.getSelectedItem().toString();
				model.getDataVector().removeAllElements();
				prizeCards=dataBase.findPrizeCardsByItem(CardName, PrizeName);
				String[] data=new String[2];
				for(PrizeCard prizeCard:prizeCards){
					
					data[0]=prizeCard.getCardName();
					data[1]=prizeCard.getPrizeName();
					model.addRow(data);
				}
				table.revalidate();
			}
		});
	}
}
